package com.thread.www;

import java.util.concurrent.locks.ReentrantReadWriteLock;

/**
 * Created by 27340 on 2017/3/21.
 */
public class ThreadLockTest {
    private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock();
    public void get(){
        rwLock.readLock().lock();
        try{
            System.out.println(Thread.currentThread().getName() + "read start");
            Thread.sleep(2000);
            System.out.println(Thread.currentThread().getName() + "read end");
        }catch (InterruptedException e){
            e.printStackTrace();
        }finally {
            rwLock.readLock().unlock();
        }
    }

    public void put(){
        rwLock.writeLock().lock();
        try{
            System.out.println(Thread.currentThread().getName() + "write start");
            Thread.sleep(1000);
            System.out.println(Thread.currentThread().getName() + "write end");
        }catch (InterruptedException e){
            e.printStackTrace();
        }finally {
            rwLock.writeLock().unlock();
        }
    }
}
